package com.baijiayun.liveuibase.toolbox.screenshare

import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.baijiayun.liveuibase.R
import com.baijiayun.liveuibase.utils.BaseUIUtils

/**
 * Created by pengwenjian on 2021/10/9
 * Describe:
 */
class ScreenShareService : Service() {
    companion object {
        const val NOTIFICATION_ID = 101
    }

    override fun onBind(intent: Intent?): IBinder? = null
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val notification = createNotification()
        startForeground(NOTIFICATION_ID, notification)
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        stopForeground(true)
    }

    private fun createNotification(): Notification {
        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notificationChannelId = "bjy_base_screen_share"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channelName = "bjy_base_screen_share_name"
            val importance = NotificationManager.IMPORTANCE_HIGH
            val notificationChannel =
                NotificationChannel(notificationChannelId, channelName, importance)
            notificationChannel.description = "bjy_base_screen_sharing"
            notificationChannel.enableVibration(false)
            notificationManager.createNotificationChannel(notificationChannel)
        }
        val builder: NotificationCompat.Builder =
            NotificationCompat.Builder(this, notificationChannelId)
        builder.setContentTitle(BaseUIUtils.getAppName(this))
        builder.setContentText(getString(R.string.bjy_base_screen_sharing))
        builder.setContentIntent(null)
        builder.setSmallIcon(R.drawable.base_ic_screen_share)
        builder.setWhen(System.currentTimeMillis())
        return builder.build()
    }
}